import {
  $post,
  $get,
  $uploadImg
} from '../utils/request'
import {
  Base64
} from 'js-base64'

// 客户自己开店铺
export const CREATE_SHOP = params => {
  return $post('/store-api/shop/createShop', params)
}

// 显示可用的店铺类型
export const FIND_SHOP_TYPE_LIST = providerId =>
  $get(`/store-api/shoptype/shopTypeList?providerId=${providerId}`)

//
export const FIND_OPEN_SHOP_STATE = (providerId, customerId) =>
  $post(
    `/store-api/shop/getLatestShopInfoByCustomerId?providerId=${providerId}&customerId=${customerId}`
  )
export const FIND_SHOP_OPEN_BY_MOBILE = (providerId, cellphone) =>
  $get(
    `/store-api/shop/findShopInfoByCellphone?providerId=${providerId}&cellphone=${cellphone}`
  )
// 查询开店模式
export const FIND_OPEN_SHOP_PAY_MODE = params =>
  $post(`/store-api/openShop/findOpenShopInfo`, params)

// 创建开店缴费支付订单
export const CREATED_OPEN_SHOP_ORDER = params =>
  $post(`/store-api/pay/createOpenShopPayOrder`, params)
// 查询开店模式
// export const FIND_OPEN_MODE = (providerId, customerId) =>
//   $get(
//     `/store-api/openShop/findOpenShopInfo?providerId=${providerId}&customerId=${customerId}`
//   )
export const FIND_OPEN_MODE = (providerId, customerId) =>
  $post(`/store-api/openShop/findOpenShopInfo`, {
    providerId,
    customerId,
    openShopType: 'OPEN'
  })
// 创建开店缴费订单
export const CREATED_SHOP_ORDER = params =>
  $post(`/store-api/openShop/createOpenShopOrder`, params)

// 获取分享图片
export const GET_SHAREIMG = (params, shareType, providerId) => {
  return $get(
    `/app-api/findPaySharePicListByShareType?appId=${params}&shareType=${shareType}&providerId=${providerId}`
  )
}

// 查询订单
export const FIND_OPEN_SHOP_ORDER_INFO = openShopOrderNo =>
  $get(
    `/store-api/openShop/findOpenShopOrderInfo?openShopOrderNo=${openShopOrderNo}`
  )

// 分页查询开店缴费订单列表
export const FIND_SHOP_OPEN_ORDER_PAGE = (params, pageNo, pageSize) =>
  $post(
    `/store-api/openShop/findOpenShopOrderPage?pageNo=${pageNo}&pageSize=${pageSize}`,
    params
  )

// 创建开店缴费订单
export const CREATE_OPEN_SHOP_ORDER = params =>
  $post('/store-api/openShop/createOpenShopOrder', params)

export const UPLOAD_IMAGE = () =>
  $uploadImg(
    '/store-api/upload-image?picType=NORMAL&catalog=small-shop',
    'picFile'
  )

// 创建开店缴费支付订单
export const CREATE_OPEN_SHOP_PAY_ORDER = params =>
  $post('/store-api/pay/createOpenShopPayOrder', params)

export const wechatGo = (r, vm, routeName, callback = () => {}) => {
  let payInfo = null
  if (r.retType === 'url') {
    window.location.href = Base64.decode(r.data)
  } else if (r.retType === 'interface') {
    payInfo = JSON.parse(Base64.decode(r.data))
    wx.requestPayment({
      timeStamp: payInfo.timeStamp.toString(),
      nonceStr: payInfo.nonceStr,
      package: payInfo.package,
      signType: payInfo.signType,
      paySign: payInfo.paySign,
      success: function (res) {
        wx.showToast({
          title: '支付成功',
          duration: 2000,
          icon: 'none'
        })
      },
      fail: function (res) {
        wx.showToast({
          title: res.errMsg,
          duration: 2000,
          icon: 'none'
        })
      },
      complete: function (res) {
        if (res.errMsg === 'requestPayment:ok') {
          wx.showToast({
            title: '支付成功',
            duration: 2000,
            icon: 'none'
          })
          setTimeout(function () {
            let url = '/pages/paySuccess/main'
            if (routeName === 'thisInOpenShop') {
              url = `/pages/paySuccess/main?isOpenShop=1`
            } else {
              url = `/pages/paySuccess/main`
            }
            wx.redirectTo({
              url,
              success: res => {},
              fail: () => {},
              complete: () => {}
            })
            // this.$router.push(`/pages/home/main`)
          }, 1000)
        } else if (res.errMsg === 'requestPayment:fail cancel') {
          wx.showToast({
            title: '支付失败',
            duration: 2000,
            icon: 'none'
          })
        }
      }
    })
  }
}
